package com.spider.lwlk.mongodb.task

class ExtendTask extends BaseTask {

    private static final BookListRegex = /<ul class="list_aa listimg"[\s\S]*?<\/ul>/

    private static final BookRegex = /"(http:\/\/product\.dangdang\.com\/\d+\.html)#.*?"/

    private static final PagerRegex =/<ul class="paging"[\s\S]*?<\/ul>/

    private static final PageRegex = /"(\/pg\d+-[.0-9a-factory]*-srsort_pubdate_desc\.html)"/

    private books = [], pages = []

    private def getBooks(def html) {

        html.eachMatch(BookRegex) { m -> books += m[1] }

        books.unique().sort()
    }

    private def getPages(def html) {

        html.eachMatch(PageRegex) { m -> pages += m[1] }

        pages = pages.collect() { page -> if(page[0] == '/') 'http://category.dangdang.com' + page else page  }

        pages.unique().sort()
    }

    @Override def onSuccess() {

        {
            arg0, arg1 ->

            [ { resp, reader ->

                def html = reader.text

                books = getBooks(html.find(BookListRegex))

                pages = getPages(html.find(PagerRegex))

                doUpdate()

            }, super.onSuccess() ].each { it.call(arg0, arg1) }
        }
    }

    @Override def onFilter() {

        {
            ->

            [ {

                books = books.findAll() { book -> !db.bookcheckedurl.findOne(url: book) }

                pages = pages.findAll() { page -> !db.pagecheckedurl.findOne(url: page) }

            }, super.onFilter() ].each { it.call() }
        }
    }

    @Override def onUpdate() {

        {
            ->

            [ {

                books.each { url -> db.bookurl.insert([ url: url ]) }

                pages.each { url -> db.pageurl.insert([ url: url ]) }

              }, super.onUpdate() ].each { it.call() }
        }
    }

    @Override def onCompleted() {

        {
            ->

            [ {

                db.pageurl.delete([ url: path ])

                db.pagecheckedurl.insert([ url: path ])

              }, super.onCompleted() ].each { it.call() }
        }
    }
}